<template>
  <div class="search-box">
    <i class="icon-search"></i>
    <input ref="query" v-model="query" class="box" :placeholder="placeholder"/>
    <i @click="clear" v-show="query" class="icon-dismiss"></i>
  </div>
</template>
<script type="text/ecmascript-6">
  import {debouce} from "../../common/js/utils";

  let _this = null;

  export default {
    props: {
      placeholder:{
        type:String,
        default:'搜索歌曲'
      }
    },
    data(){
      return {
        query:''
      }
    },
    created(){
      //debounce应用场景：根据用户的输入实时向服务器发 ajax 请求获取数据
      this.$watch('query',debouce((newQuery) => {
        //派发事件
        this.$emit('query',newQuery)
      },200))
    },
    mounted () {
      _this = this;
      _this.init();
    },
    methods: {
      init () {
      },
      clear(){
        this.query = ''
      },
      setQuery(query){
        this.query = query
      },
      //失去焦点
      blur(){
        this.$refs.query.blur()
      }
    },
    watch: {},
    components: {},
    computed: {},

  }
</script>


<style lang="stylus" rel="stylesheet/stylus">
  @import '../../common/stylus/variable.styl';
  .search-box
    display: flex
    align-items: center
    box-sizing: border-box
    width: 100%
    padding: 0 6px
    height: 40px
    background: $color-highlight-background
    border-radius: 6px
    .icon-search
      font-size: 24px
      color: $color-background
    .box
      flex: 1
      margin: 0 5px
      line-height: 18px
      background: $color-highlight-background
      color: $color-text
      font-size: $font-size-medium
      &::placeholder
        color: $color-text-d
    .icon-dismiss
      font-size: 16px
      color: $color-background
</style>
